package dao;

import java.io.File;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.dom4j.Document;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;

import utils.DBUtils;

import com.opensymphony.xwork2.ActionSupport;

import dao.impls.IDataServiceDAO;


public class DataServiceDAOImpl extends DataServiceDAOImplParticular implements IDataServiceDAO {

	/* (non-Javadoc)
	 * @see dao.DataService#getPageXml(java.lang.String)
	 */
	@Override
	public String getPageXml(String Path) throws Exception {
		// TODO Auto-generated method stub
	    File xmlFile = new File(Path);
        DocumentBuilderFactory builderFactory =  DocumentBuilderFactory.newInstance();   
        DocumentBuilder builder = builderFactory.newDocumentBuilder();  
        org.w3c.dom.Document doc = builder.parse(xmlFile);
        doc.getDocumentElement().normalize();
        String jsonResult = doc.getElementsByTagName("Config").item(0).getTextContent();
	    return jsonResult;
	}

	/* (non-Javadoc)
	 * @see dao.DataService#getData(java.lang.String)
	 */
	@Override
	public List getData(String datasetId) {
		// TODO Auto-generated method stub
		Connection con = null;
		PreparedStatement prep = null;
		ResultSet rs = null;
		try {
			String sql = XpathToParseXmlInDataSet(datasetId);
			con = DBUtils.getConnection();
			prep = con.prepareStatement(sql);
			rs = prep.executeQuery();
			ResultSetMetaData rsmd=rs.getMetaData();
			int count = rsmd.getColumnCount();
			List list = new ArrayList();
			while(rs.next()){
				Map map=new HashMap();
				for(int i=1;i<=count;i++){
				//获取指定列的表目录名称
				String label=rsmd.getColumnLabel(i);
				//以 Java 编程语言中 Object 的形式获取此 ResultSet 对象的当前行中指定列的值
				Object object= rs.getObject(i);
				//把数据库中的字段名和值对应为一个map对象中的一个键值对
				map.put(label.toLowerCase(), object);
				}
				//把每条对象封装成的map对象放进list中
				list.add(map);
			}
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtils.closeConnection(con);
		}
		return null;
	}
}


class DataServiceDAOImplParticular{
	
	public String XpathToParseXmlInDataSet(String dataSetId) throws Exception{
		SAXReader saxReader = new SAXReader();
		ActionSupport actionSupport =new ActionSupport();
		String baseUrl = actionSupport.getText("fileDisk.baseUrl");
		String DataSetManager = actionSupport.getText("fileDisk.DataSetManager");
		String filePath = baseUrl  +"/"+ DataSetManager + "/DataSet.xml";

		Document doc = saxReader.read(new File(filePath));
		String xpathStr1 = "//DataSet[ID='" + dataSetId + "']//DataSource";
		String xpathStr2 = "//DataSet[ID='" + dataSetId + "']//VirtualTable";
		String DataSource = doc.selectSingleNode(xpathStr1).getText();
		String VirtualTable = doc.selectSingleNode(xpathStr2).getText();
		return XpathToParseXmlInEntities(DataSource, VirtualTable);
	}
	
	public String XpathToParseXmlInEntities(String DataSource, String dataSouseId) throws  Exception {
		SAXReader saxReader = new SAXReader();	
		ActionSupport actionSupport =new ActionSupport();
		String baseUrl = actionSupport.getText("fileDisk.baseUrl");
		String filePath = baseUrl  +"/"+ DataSource + "/Entities.xml";
		
		Document doc = saxReader.read(new File(filePath));
		Map<String, String> map = new HashMap<String, String>();
		map.put("xmlns", "http://tempuri.org/EntityInfo.xsd");
		saxReader.getDocumentFactory().setXPathNamespaceURIs(map);
		String xpathStr = "//xmlns:SingleEntityInfo[xmlns:ID='" + dataSouseId + "']//xmlns:SqlString";
		XPath xpath = doc.createXPath(xpathStr);
		xpath.setNamespaceURIs(map);
		String sqlStr = xpath.selectSingleNode(doc).getText();
		return sqlStr;
	}

}
